#include <allegro.h>
#include <time.h>
#include "general.h"
#include <stdio.h>
#include <stdlib.h>
#define NB_IMAGES 3



int main() 
{
int h,j=0,x=0,x1=0,somme_bonus=0,test =0,pause=0,temp_pause=0;
time_t t,t1,t2=0,t3=0;
BITMAP *soul; //soul of the ennemie
BITMAP *Buffer;
BITMAP *im1; //Jump right
BITMAP *im2;// Jump left
BITMAP *intro1;// pics of the intro
BITMAP *intro2;
BITMAP *intro3;
BITMAP *intro4;
BITMAP *intro5;
BITMAP *intro6;
BITMAP *intro7;
BITMAP *intro8;
BITMAP *intro9;
BITMAP *intro10;
BITMAP *intro11;
BITMAP *intro12;
BITMAP *intro13;
BITMAP *intro14;// pics of the intro
BITMAP *menu;//Menu
BITMAP *bon;//pic of the bonus
BITMAP *Background;//Background
BITMAP *backgroundcol;//Background collision
BITMAP *gameover;//Game over pic
BITMAP *B; // Pic of the Bonus wich is drawed upside
SAMPLE *sound;
SAMPLE *sound1;
SAMPLE *sound2;
SAMPLE *sound3;
SAMPLE *sound4;
SAMPLE *sound5;
SAMPLE *intro;
int teste1=0;//deplecemen's test  of ennemie 1
int testee=0;//deplecemen's test  of ennemie 
int test_soul=0;
int test_soul1=0;
perso p ;//Our hero the pirate
perso e,e1 ;//ennemies
bonus b ;
bonus b1,b2,b3 ;//Bonus
int i,test_chargement=0;
int compteur;
float poz=0;
bonus ball;
int cartouche=0;
FILE *F=NULL;

allegro_init();

if (install_keyboard() == -1) {
allegro_message("Error! %s", allegro_error);
return -1;
}

set_color_depth(16);


if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 1300,680, 0, 0) != 0) {
set_gfx_mode(GFX_TEXT, 0, 0, 0, 0); /* here’s the command for text mode */
allegro_message("Unable to initialize video mode!\n%s\n", allegro_error);
return 1;
}

if(install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL)) {
  allegro_message("Sound Error: %s", allegro_error);
  
}

 

sound = load_sample("sound1.wav");
sound1 = load_sample("ennemie.wav");
sound2 = load_sample("jump.wav");
sound3 = load_sample("BONUS.wav");
sound4 = load_sample("power0.wav");
sound5 = load_sample("histor.wav");
intro = load_sample("intro.wav");
p.nv=3;
compteur=0;
chargement (&p,&b,&e,&b1); 
initialisation(&p,&b,&e,&e1,&b1,&b2,&b3);
Background = load_bitmap("./images/back1.bmp", NULL);
backgroundcol = load_bitmap("./images/backgroundcol.bmp", NULL);
menu = load_bitmap("./images/menu.bmp", NULL);
gameover = load_bitmap("./images/gameover.bmp", NULL);
ball.bon = load_bitmap("./images/ball.bmp", NULL);
bon = load_bitmap("./images/bonus.bmp", NULL);
im1 = load_bitmap("./images/jumpr.bmp", NULL);
im2 = load_bitmap("./images/jumpl.bmp", NULL);
soul = load_bitmap("./images/soul.bmp", NULL);
intro1 = load_bitmap("./images/a1.bmp", NULL);
intro2 = load_bitmap("./images/a2.bmp", NULL);
intro3 = load_bitmap("./images/a3.bmp", NULL);
intro4 = load_bitmap("./images/a4.bmp", NULL);
intro5 = load_bitmap("./images/a5.bmp", NULL);
intro6 = load_bitmap("./images/a6.bmp", NULL);
intro7 = load_bitmap("./images/a7.bmp", NULL);
intro8 = load_bitmap("./images/a8.bmp", NULL);
intro9 = load_bitmap("./images/a9.bmp", NULL);
intro10 = load_bitmap("./images/a10.bmp", NULL);
intro11= load_bitmap("./images/a11.bmp", NULL);

intro12 = load_bitmap("./images/h1.bmp", NULL);
intro13 = load_bitmap("./images/h2.bmp", NULL);
intro14 = load_bitmap("./images/h3.bmp", NULL);
B = load_bitmap("./images/bonus1.bmp", NULL);

/*

clear_bitmap(screen);
Buffer = create_bitmap(1300, 680);

son(intro);

blit(intro1, Buffer, 0, 0, 0, 0, 1300, 600);
blit( Buffer,screen, 0, 0, 0, 0, 1300, 600);
sleep(1);
blit(intro2, Buffer, 0, 0, 0, 0, 1300, 680);
blit( Buffer,screen, 0, 0, 0, 0, 1300, 680);
sleep(1);
blit(intro3, Buffer, 0, 0, 0, 0, 1300, 680);
blit( Buffer,screen, 0, 0, 0, 0, 1300, 680);
sleep(1);
blit(intro4, Buffer, 0, 0, 0, 0, 1300, 680);
blit( Buffer,screen, 0, 0, 0, 0, 1300, 680);
sleep(1);
blit(intro5, Buffer, 0, 0, 0, 0, 1300, 680);
blit( Buffer,screen, 0, 0, 0, 0, 1300, 680);
sleep(1);
blit(intro6, Buffer, 0, 0, 0, 0, 1300, 680);
blit( Buffer,screen, 0, 0, 0, 0, 1300, 680);
sleep(1);
blit(intro7, Buffer, 0, 0, 0, 0, 1300, 680);
blit( Buffer,screen, 0, 0, 0, 0, 1300, 680);
sleep(1);
blit(intro8, Buffer, 0, 0, 0, 0, 1300, 680);
blit( Buffer,screen, 0, 0, 0, 0, 1300, 680);
sleep(1);
blit(intro10, Buffer, 0, 0, 0, 0, 1300, 680);
blit( Buffer,screen, 0, 0, 0, 0, 1300, 680);
sleep(1);
blit(intro11, Buffer, 0, 0, 0, 0, 1300, 680);
blit( Buffer,screen, 0, 0, 0, 0, 1300, 680);
sleep(2);
blit(intro9, Buffer, 0, 0, 0, 0, 1300, 680);
blit( Buffer,screen, 0, 0, 0, 0, 1300, 680);
sleep(4);

stop_sample(intro);

*/

clear_bitmap(screen);
Buffer = create_bitmap(1300, 680);

while (!key[KEY_S] ) {
son1 (sound);
blit(menu, Buffer, 0, 0, 0, 0, 1300, 680);
blit( Buffer,screen, 0, 0, 0, 0, 1300, 680);

if(key[KEY_E] )
{destroy_bitmap(screen);
allegro_exit();}

if(key[KEY_C] )
{test_chargement=1;}


if(key[KEY_H] )
{stop_sample(sound);
son(sound5);
blit(intro12, Buffer, 0, 0, 0, 0, 1300, 680);
blit( Buffer,screen, 0, 0, 0, 0, 1300, 680);
sleep(4);
blit(intro13, Buffer, 0, 0, 0, 0, 1300, 680);
blit( Buffer,screen, 0, 0, 0, 0, 1300, 680);
sleep(4);
blit(intro14, Buffer, 0, 0, 0, 0, 1300, 680);
blit( Buffer,screen, 0, 0, 0, 0, 1300, 680);
sleep(5);
stop_sample(sound5);
}
}
if(test_chargement ==1 )
{charger_partie(F,&p,&e,&e1,Background,backgroundcol,&b,&b1,&b2,&b3,&somme_bonus,&poz,&test);
//allegro_message("chargement réussi");
printf("p.x :%0.0f\n p.y : %0.0f\n p.direction : %d \np.power : %d \ne.x : %0.0f \n e.y : %0.0f\n e.direction :  %d \n e.power : %d\ne1.x %0.0f \n e1.y : %0.0f \ne1.direction : %d \ne1.power : %d\n b.x : %d \n b.y : %d \nb.n : %d \nb1.x : %d \n b1.y : %d \nb1.n : %d \nb2.x : %d\n b2.y : %d \nb2.n:  %d\nb3.x :  %d \n b3.y : %d \n b3.n :  %d\n somme_bonus : %d\n poz : %0.0f \n test : %d",p.x,p.y,p.direction,p.power,p.x,e.y,e.direction,e.power,e1.x,e1.y,e1.direction,e1.power,b.x,b.y,b.n,b1.x,b1.y,b1.n,b2.x,b2.y,b2.n,b3.x,b3.y,b3.n,somme_bonus,poz,test);
}



 
destroy_bitmap(Buffer);
Buffer = create_bitmap(1300, 681);

ball.n=0;
ball.x=p.x;
ball.y=p.y+20;
time(&t1);
do{
while (!key[KEY_ESC]&&p.nv>0&& pause==0) {



//while(pause==0&& p.nv>0){
time(&t);
temp_pause = difftime(t,t1);
if(key[KEY_P])
{allegro_message("Pause !");
time(&t2);
pause=1;

}


blit(Background, Buffer, poz, 0, 0, 0, Background->w, Background->h);

		move_pirate(&poz,&test,&p,&compteur,backgroundcol);

textprintf_centre_ex(Buffer, font, 700,50,makecol(0, 0 ,0), -1,"Lifes : %d",p.nv);
textprintf_centre_ex(Buffer, font, 1110,50,makecol(0, 0 ,0), -1,"POWER : %d",p.power);
if ( pause==0)
textprintf_centre_ex(Buffer, font, 100,50,makecol(0, 0 ,0), -1,"TIME : %0.0f",difftime(t,t1)-difftime(t3,t2));
else
textprintf_centre_ex(Buffer, font, 100,50,makecol(0, 0 ,0), -1,"TIME : %d",temp_pause);


//textprintf_centre_ex(Buffer, font, 100,150,makecol(0, 0 ,0), -1," difference TIME : %0.0f",difftime(t3,t2));
textprintf_centre_ex(Buffer, font, 450,50,makecol(0, 0 ,0), -1,"SCORE : %d",somme_bonus);



draw_sprite(Buffer, B, 350, 25);//dessiner l'image du bonus près du score

if (ball.x>SCREEN_W||ball.x<0)
ball.x=p.x;

//somme_bonus+=difftime(t,t1);

int sprite1_x=p.x;
int sprite1_y=p.y;
int sprite1_w=p.img_right[0]->w;
int sprite1_h=p.img_right[0]->h;

int sprite3_x=e.x;
int sprite3_y=p.y;
int sprite3_w=p.img_right[0]->w;
int sprite3_h=p.img_right[0]->h;

int sprite2_x=b.x;
int sprite2_y=b.y;
int sprite2_w=b.bon->w;
int sprite2_h=b.bon->h;

int sprite4_x=b1.x;
int sprite4_y=b1.y;
int sprite4_w=b.bon->w;
int sprite4_h=b.bon->h;

int sprite5_x=b2.x;
int sprite5_y=b2.y;
int sprite5_w=b.bon->w;
int sprite5_h=b.bon->h;

int sprite6_x=b3.x;
int sprite6_y=b3.y;
int sprite6_w=b.bon->w;
int sprite6_h=b.bon->h;

int sprite7_x=e1.x;
int sprite7_y=p.y;
int sprite7_w=p.img_right[0]->w;
int sprite7_h=p.img_right[0]->h;

int sprite8_x=ball.x;
int sprite8_y=ball.y;
int sprite8_w=ball.bon->w;
int sprite8_h=ball.bon->h;


if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,sprite2_x, sprite2_y, sprite2_w, sprite2_h )==1){
b.n=1;
son(sound3);
b.x=-3000;

somme_bonus+=300;
}
if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,sprite4_x, sprite4_y, sprite4_w, sprite4_h )==1){
b1.n=1;
son(sound3);
b1.x=-3000;
somme_bonus+=300;
}

if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,sprite5_x, sprite5_y, sprite5_w, sprite5_h )==1){
b2.n=1;
son(sound3);
b2.x=-3000;
somme_bonus+=300;
}
if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,sprite6_x, sprite6_y, sprite6_w, sprite6_h )==1){
b3.n=1;
son(sound3);
b3.x=-3000;
somme_bonus+=300;
}
if(bounding_box_collision(sprite1_x+poz, sprite1_y, sprite1_w, sprite1_h,sprite3_x, sprite3_y, sprite3_w, sprite3_h )==1){
son (sound1);
e.power-=20;
p.power-=55;
if (e1.power<=0){
e.nv=1;
e.x=-300000;
somme_bonus+=1000;}
}
if(bounding_box_collision(sprite1_x+poz, sprite1_y, sprite1_w, sprite1_h,sprite7_x, sprite7_y, sprite7_w, sprite7_h )==1 )
{
p.power-=55;
son (sound1);
e1.power-=20;
if (e1.power<=0){
e1.nv=1;
e1.x=-10000000;
somme_bonus+=1000;}
}

if(bounding_box_collision(sprite8_x+poz, sprite8_y, sprite8_w, sprite8_h,sprite3_x, sprite3_y, sprite3_w, sprite3_h )==1 )
{cartouche=0;
son (sound1);
e.power-=50;
if (e.power<=0){
e.nv=1;
test_soul=1;
}
somme_bonus+=1000;
ball.x=p.x;
}

if(bounding_box_collision(sprite8_x+poz, sprite8_y, sprite8_w, sprite8_h,sprite7_x, sprite7_y, sprite7_w, sprite7_h )==1 )
{cartouche=0;
son (sound1);
e1.power-=50;
if (e1.power<=0){
e1.nv=1;
test_soul1=1;}
somme_bonus+=1000;
ball.x=p.x;
}

if(test_soul==1)
{ if(e.y>-100)
e.y-=3;
e.x+=3;
draw_sprite(Buffer, soul, e.x-poz, e.y);
if (e.y<=-100)
e.x=-48641534186415;
}

if(test_soul1==1)
{ if(e1.y>-100)
e1.y-=3;
e1.x+=3;
draw_sprite(Buffer, soul, e1.x-poz, e1.y);
if (e1.y<=-100)
e1.x=-48641534186415;
}


 if (somme_bonus>=5000)
{somme_bonus=0;
p.nv++;
}


if(e.nv==0){
    if (e.x>3730 && testee ==0)
{e.x--;
draw_sprite(Buffer, e.img_left[j%21/7], e.x-poz, e.y);
j++;
usleep(700);}


    if (e1.x<4650 && testee ==1)
{e.x++;
draw_sprite(Buffer, e.img_right[j%21/7], e.x-poz, e.y);
j++;
usleep(700);}

if (e.x==3731)
testee=1;
if (e.x==4649)
testee=0;
}

if(e1.nv==0){
    if (e1.x>850 && teste1 ==0)
{e1.x--;
draw_sprite(Buffer, e.img_left[j%21/7], e1.x-poz, e1.y);
j++;
usleep(700);}


    if (e1.x<1600 && teste1 ==1)
{e1.x++;
draw_sprite(Buffer, e.img_right[j%21/7], e1.x-poz, e1.y);
j++;
usleep(700);}

if (e1.x==851)
teste1=1;
if (e1.x==1599)
teste1=0;
}

if(b1.n==0){
draw_sprite(Buffer, b.bon, b1.x, b1.y);
}

if(b.n==0){
draw_sprite(Buffer, b.bon, b.x, b.y);
}
if(b2.n==0){
draw_sprite(Buffer, b.bon, b2.x, b2.y);
}
if(b3.n==0){
draw_sprite(Buffer, b.bon, b3.x, b3.y);
}


if (key[KEY_RIGHT] && p.x<SCREEN_W - p.img_left[0]->w && collision_background_right(poz,p,backgroundcol)==0) {
i++;
p.x+=0.5;
p.direction=0;
//e1.x-=1.5+2;
//e.x-=1.5+2;

if (p.x>0)
{
poz+=3;
b.x-=3;
b1.x-=3;
b2.x-=3;
b3.x-=3;
}

}

if (key[KEY_LEFT] && p.x > 3 && collision_background_left(poz,p,backgroundcol)==0) {
i++;
p.x-=0.5;
p.direction=1;
//e1.x-=0.5-3;
//e.x-=0.5-3;

if(poz>0)
{poz-=3;
b.x+=3;
b1.x+=3;
b2.x+=3;
b3.x+=3;

}}

if(key[KEY_X])
{cartouche=1;
//ball.x=p.x;
}
if(key[KEY_C])
{cartouche=0;
ball.x=p.x;
}


if(key[KEY_E])
{enregistrer(F,&p,&e,&e1,Background,backgroundcol,&b,&b1,&b2,&b3,somme_bonus,poz,test);
allegro_message("Enregistrement terminé !");

}



if (p.direction==0){ //allegro_message("test");
draw_sprite(Buffer,p.img_right[i%21/7], p.x, p.y);   
   
}

else if (p.direction==1){draw_sprite(Buffer, p.img_left[i%21/7], p.x, p.y);      
 }

else if (p.direction==3){

draw_sprite(Buffer,im1, p.x, p.y);


}

else if (p.direction==4){
draw_sprite(Buffer,im2, p.x, p.y);


}


if (cartouche==1)
{ if (p.direction==0)
{if(ball.x<=SCREEN_W)
{ball.x+=20;
draw_sprite(Buffer,ball.bon, ball.x, p.y);}}
else
{if(ball.x>0){

ball.x-=10;
draw_sprite(Buffer,ball.bon, ball.x, p.y);}}
cartouche=0;
}

blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);//copy the buffer

if(compteur==1000){
compteur=0;

}
if (p.power<=0 && p.nv>0)
{p.nv--;
initialisation(&p,&b,&e,&e1,&b1,&b2,&b3);
poz=0;
test_soul=0;
test_soul1=0;
}




//}
}
if(key[KEY_R])
{allegro_message("Partie reprise !");
pause=0;
time(&t3);
}



}while(!key[KEY_ESC] &&p.nv>0 );

destroy_bitmap(Buffer);
if (p.nv==0)
{stop_sample(sound);
clear_bitmap(screen);
Buffer = create_bitmap(1300,680);
blit(gameover, Buffer, 0, 0, 0, 0,1300,680);
blit( Buffer,screen, 0, 0, 0, 0, 1300,680);
son(sound4);
sleep(8);
}





return 0;
}
END_OF_MAIN();


